HAL OSPI initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 OSPI configuration panel.

  • Configure the SW layer HAL.

  • Configure the OSPI main features.

  • Configure the advanced OSPI features if needed.

  • Configure the OSPI GPIO pins.

  • Configure the OSPI DMA(s) if the application requires to use the OSPI in DMA mode.

  • Configure the NVIC if the application requires the OSPI to be used in IT mode.

  • Generate the code.

Note

In HAL2, the XSPI driver now allows addressing quad, octal, and hexa memories(No more OSPI driver in HAL2)

HAL OSPI configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL OSPI main features configuration

Configure the OSPI main features.

OSPI main feature configuration

HAL OSPI advanced features configuration

Configure the advanced OSPI features if needed.

OSPI advanced feature configuration

GPIO configuration

Configure GPIO pins.

Note

The GPIO configuration is managed in XSPIM

Main features configuration

OSPI GPIO port main configuration OSPI GPIO configuration

DMA configuration

Configure the DMA for data reception as needed.

OSPI DMA configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

OSPI NVIC configuration

Generated OSPI initialization and de-initialization sequences

The OSPI initialization sequence is generated in the mx_xspi1_init function in mx_xspi1.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated : OSPI Initialization and de-initialization sequences

Topic

HAL1 code snippet

HAL2 code snippet

Initialization and configuration sequence

static void MX_OCTOSPI1_Init(void)
{
    OSPIM_CfgTypeDef sOspiManagerCfg = {0};
    HAL_OSPI_DLYB_CfgTypeDef HAL_OSPI_DLYB_Cfg_Struct = {0};
    /* OCTOSPI1 parameter configuration*/
    hospi1.Instance = OCTOSPI1;
    hospi1.Init.FifoThreshold = 1;
    hospi1.Init.DualQuad = HAL_OSPI_DUALQUAD_DISABLE;
    hospi1.Init.MemoryType = HAL_OSPI_MEMTYPE_MICRON;
    hospi1.Init.DeviceSize = 32;
    hospi1.Init.ChipSelectHighTime = 1;
    hospi1.Init.FreeRunningClock = HAL_OSPI_FREERUNCLK_DISABLE;
    hospi1.Init.ClockMode = HAL_OSPI_CLOCK_MODE_0;
    hospi1.Init.WrapSize = HAL_OSPI_WRAP_NOT_SUPPORTED;
    hospi1.Init.ClockPrescaler = 1;
    hospi1.Init.SampleShifting = HAL_OSPI_SAMPLE_SHIFTING_NONE;
    hospi1.Init.DelayHoldQuarterCycle = HAL_OSPI_DHQC_DISABLE;
    hospi1.Init.ChipSelectBoundary = 0;
    hospi1.Init.DelayBlockBypass = HAL_OSPI_DELAY_BLOCK_BYPASSED;
    hospi1.Init.MaxTran = 0;
    hospi1.Init.Refresh = 0;
    if (HAL_OSPI_Init(&hospi1) != HAL_OK)
    {
        Error_Handler();
    }
    sOspiManagerCfg.DQSPort = 1;
    sOspiManagerCfg.NCSPort = 1;
    sOspiManagerCfg.IOLowPort = HAL_OSPIM_IOPORT_1_LOW;
    if (HAL_OSPIM_Config(&hospi1, &sOspiManagerCfg, HAL_OSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
    {
        Error_Handler();
    }

































    HAL_OSPI_DLYB_Cfg_Struct.Units = 0;
    HAL_OSPI_DLYB_Cfg_Struct.PhaseSel = 0;
    if (HAL_OSPI_DLYB_SetConfig(&hospi1, &HAL_OSPI_DLYB_Cfg_Struct) != HAL_OK)
    {
        Error_Handler();
    }
}




void HAL_OSPI_MspInit(OSPI_HandleTypeDef* hospi)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if(hospi->Instance==OCTOSPI1)
{
/** Initializes the peripherals clock */
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_OSPI;
    PeriphClkInit.OspiClockSelection = RCC_OSPICLKSOURCE_SYSCLK;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
    Error_Handler();
    }

    /* Peripheral clock enable */
    __HAL_RCC_OSPIM_CLK_ENABLE();
    __HAL_RCC_OSPI1_CLK_ENABLE();

    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();

    /**OCTOSPI1 GPIO Configuration
        PC0     ------> OCTOSPIM_P1_IO7
        PC1     ------> OCTOSPIM_P1_IO4
        PC2     ------> OCTOSPIM_P1_IO5
        PA7     ------> OCTOSPIM_P1_IO2
        PC3     ------> OCTOSPIM_P1_IO6
        PB0     ------> OCTOSPIM_P1_IO1
        PA1     ------> OCTOSPIM_P1_DQS
        PA2     ------> OCTOSPIM_P1_NCS
        PA3     ------> OCTOSPIM_P1_CLK
        PA6     ------> OCTOSPIM_P1_IO3
        PB1     ------> OCTOSPIM_P1_IO0
        PB12    -------> OCTOSPIM_P1_NCLK
        */

    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF3_OCTOSPI1;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF10_OCTOSPI1;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 |GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF10_OCTOSPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF10_OCTOSPI1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

}
hal_xspi_handle_t *mx_octospi1_hal_init(void)
{
  hal_xspi_config_t xspi_config;
  hal_xspi_memory_config_t memory_config;
  hal_xspi_timing_config_t timing_config;
  uint32_t p_max_clock_phase;















  if (HAL_XSPI_Init(&hOCTOSPI1, HAL_OCTOSPI1) != HAL_OK)
  {
    return NULL;
  }

  HAL_RCC_OCTOSPI1_EnableClock();
  HAL_RCC_GPIOP_EnableClock();
  HAL_RCC_GPIOO_EnableClock();
  if (HAL_RCC_OCTOSPI_SetKernelClkSource(HAL_RCC_OCTOSPI_CLK_SRC_SYSCLK) != HAL_OK)
  {
    return NULL;
  }
  /* Main peripheral configuration */
  memory_config.mode = HAL_XSPI_MEMORY_SINGLE;
  memory_config.clock_polarity = HAL_XSPI_CLOCK_LOW;
  memory_config.type = HAL_XSPI_MEMORY_TYPE_MICRON;
  memory_config.size_bit = HAL_XSPI_MEMORY_SIZE_16BIT;
  memory_config.wrap_size_byte = HAL_XSPI_WRAP_NOT_SUPPORTED;
  memory_config.cs_boundary = HAL_XSPI_CS_BOUNDARY_NONE;

  timing_config.clk_prescaler = 0x1U;
  timing_config.shift = HAL_XSPI_SAMPLE_SHIFT_NONE;
  timing_config.hold = HAL_XSPI_DELAY_HOLD_NONE;
  timing_config.cs_high_time_cycle = 0x1U;
  timing_config.cs_release_time_cycle = 0x0U;
  timing_config.cs_refresh_time_cycle = 0x0U;
  timing_config.dlyb_state = HAL_XSPI_DLYB_ON;

  xspi_config.memory = memory_config;
  xspi_config.timing = timing_config;

  if (HAL_XSPI_SetConfig(&hOCTOSPI1, &xspi_config) != HAL_OK)
  {
    return NULL;
  }

  /* Delay block configuration */
  if (HAL_XSPI_DLYB_Enable(&hOCTOSPI1) != HAL_OK)
  {
    return NULL;
  }
  if (HAL_XSPI_DLYB_CalculateMaxClockPhase(&hOCTOSPI1, &p_max_clock_phase) != HAL_OK)
  {
    return NULL;
  }

  if (HAL_XSPI_DLYB_SetConfigDelay(&hOCTOSPI1, p_max_clock_phase) != HAL_OK)
  {
    return NULL;
  }

  return &hOCTOSPI1;
}


system_status_t mx_octospim_hal_init(void)
{
  hal_xspi_xspim_config_t xspim_config;

  HAL_RCC_OCTOSPIM_EnableClock();

  xspim_config.port1.clk = HAL_XSPI_XSPIM_XSPI1;
  xspim_config.port1.dqs = HAL_XSPI_XSPIM_XSPI1;
  xspim_config.port1.ncs = HAL_XSPI_XSPIM_XSPI1;
  xspim_config.port1.io_low = HAL_XSPI_XSPIM_XSPI1_IO_LOW;
  xspim_config.port1.io_high = HAL_XSPI_XSPIM_XSPI1_IO_HIGH;

  xspim_config.port2.clk = HAL_XSPI_XSPIM_NONE;
  xspim_config.port2.dqs = HAL_XSPI_XSPIM_NONE;
  xspim_config.port2.ncs = HAL_XSPI_XSPIM_NONE;
  xspim_config.port2.io_low = HAL_XSPI_XSPIM_IO_NONE;
  xspim_config.port2.io_high = HAL_XSPI_XSPIM_IO_NONE;

  xspim_config.req_ack_cycle = 0x0UL;

  if (HAL_XSPI_XSPIM_SetConfig(&xspim_config) != HAL_OK)
  {
    return SYSTEM_PERIPHERAL_ERROR;
  }


  HAL_RCC_GPIOA_EnableClock();
  HAL_RCC_GPIOB_EnableClock();
  HAL_RCC_GPIOC_EnableClock();
  hal_gpio_config_t  gpio_config;

  /**
    OCTOSPIM GPIO Configuration
    PA3   --------->  OCTOSPIM_P1_CLK
    PA1   --------->  OCTOSPIM_P1_DQS
    PA2   --------->  OCTOSPIM_P1_NCS
    PA7   --------->  OCTOSPIM_P1_IO2
    PA6   --------->  OCTOSPIM_P1_IO3
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_3 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_7 | HAL_GPIO_PIN_6, &gpio_config);


  /**
    OCTOSPIM GPIO Configuration
    PB12  --------->  OCTOSPIM_P1_NCLK
    PB1   --------->  OCTOSPIM_P1_IO0
    PB0   --------->  OCTOSPIM_P1_IO1
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_12 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_0, &gpio_config);


  /**
    OCTOSPIM GPIO Configuration
    PC1   --------->  OCTOSPIM_P1_IO4
    PC2   --------->  OCTOSPIM_P1_IO5
    PC3   --------->  OCTOSPIM_P1_IO6
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOC, HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_3, &gpio_config);


  /**
    OCTOSPIM GPIO Configuration
    PC0   --------->  OCTOSPIM_P1_IO7
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_3;
  HAL_GPIO_Init(HAL_GPIOC, HAL_GPIO_PIN_0, &gpio_config);

  return SYSTEM_OK;
}

Deinitialization sequence

static void MX_OCTOSPI1_DeInit(void)
{
    HAL_OSPI_DeInit(OCTOSPI1);
}

void HAL_OSPI_MspDeInit(OSPI_HandleTypeDef* hospi)
{
    if(hospi->Instance==OCTOSPI1)
    {
        /* Peripheral clock disable */
        __HAL_RCC_OSPI1_CLK_DISABLE();

        /**OCTOSPI1 GPIO Configuration*/
         HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
         HAL_GPIO_DeInit(GPIOA, GPIO_PIN_7|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 |GPIO_PIN_6);
         HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_12);
    }
}
void mx_octospi1_hal_deinit(void)
{
    (void)HAL_XSPI_DeInit(&hOCTOSPI1);
    HAL_RCC_OCTOSPI1_Reset();
    HAL_RCC_OCTOSPI1_DisableClock();
}